package com.zlp.mall.modules.oms.controller;


import com.zlp.mall.common.api.Pager;
import com.zlp.mall.common.api.R;
import com.zlp.mall.modules.oms.dto.*;
import com.zlp.mall.modules.oms.model.Order;
import com.zlp.mall.modules.oms.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author ZouLiPing
 * @since 2021-11-03
 */
@RestController
@Api(value = "OmsOrderController", tags = "订单管理")
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("查询订单")
    @GetMapping(value = "/list")
    public R<Pager<Order>> list(OrderQueryParam queryParam,
                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Pager<Order> pager = orderService.list(queryParam, pageSize, pageNum);
        return R.success(pager);
    }

    @ApiOperation("批量发货")
    @GetMapping(value = "/update/delivery")
    public R delivery(@RequestBody List<OrderDeliveryParam> deliveryParamList) {
        boolean result = orderService.delivery(deliveryParamList);
        if (result) {
            return R.success();
        }
        return R.failed();
    }

    @ApiOperation("批量关闭订单")
    @GetMapping(value = "/update/close")
    public R close(@RequestParam("ids") List<Long> ids, @RequestParam String note) {
        boolean result = orderService.close(ids, note);
        if (result) {
            return R.success();
        }
        return R.failed();
    }

    @ApiOperation("批量删除订单")
    @PostMapping(value = "/delete")
    public R delete(@RequestParam("ids") List<Long> ids) {
        boolean result = orderService.delete(ids);
        if (result) {
            return R.success();
        }
        return R.failed();
    }

    @ApiOperation("获取订单详情:订单信息、商品信息、操作记录")
    @GetMapping(value = "/{id}")
    public R<OmsOrderDetail> detail(@PathVariable Long id) {
        OmsOrderDetail orderDetailResult = orderService.detail(id);
        return R.success(orderDetailResult);
    }

    @ApiOperation("修改收货人信息")
    @PostMapping(value = "/update/receiverInfo")
    public R updateReceiverInfo(@RequestBody OmsReceiverInfoParam receiverInfoParam) {
        int count = orderService.updateReceiverInfo(receiverInfoParam);
        if (count > 0) {
            return R.success(count);
        }
        return R.failed();
    }

    @ApiOperation("修改订单费用信息")
    @PostMapping(value = "/update/moneyInfo")
    public R updateReceiverInfo(@RequestBody OmsMoneyInfoParam moneyInfoParam) {
        int count = orderService.updateMoneyInfo(moneyInfoParam);
        if (count > 0) {
            return R.success(count);
        }
        return R.failed();
    }

    @ApiOperation("备注订单")
    @PostMapping(value = "/update/note")
    public R updateNote(@RequestParam("id") Long id,
                                   @RequestParam("note") String note,
                                   @RequestParam("status") Integer status) {
        Boolean result = orderService.updateNote(id, note, status);
        if (result) {
            return R.success(result);
        }
        return R.failed();
    }

}